import React, { useEffect, useState } from 'react';
import {
  EllipsisOutlined,
  PlusOutlined,
  AreaChartOutlined,
} from '@ant-design/icons';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { Button, Dropdown, Row, Col, Table, Input } from 'antd';
import { useRef } from 'react';
import { timestampToTime } from '@/utils/index';
import { Switch, Tooltip } from 'antd';
import Lazyload from 'react-lazyload';
import { history, connect } from 'umi';
type GithubIssueItem = {
  url: string;
  id: number;
  number: number;
  title: string;
  labels: {
    name: string;
    color: string;
  }[];
  state: string;
  comments: number;
  created_at: string;
  updated_at: string;
  closed_at?: string;
};

function houseManagement(props: any) {
  const columns: ProColumns<GithubIssueItem>[] = [
    {
      title: 'ID',
      dataIndex: 'id',
    },
    {
      title: '楼盘名称',
      dataIndex: 'title',
      render: (item, info: any) => {
        const img =
          info.rec_position === 1 ? (
            <img src="http://fangadmin.shbwyz.com/static/images/rec.png"></img>
          ) : (
            ''
          );
        return (
          <React.Fragment>
            {item}
            <Tooltip
              title={
                // 懒加载
                <Lazyload>
                  <img
                    src={`http://fangadmin.shbwyz.com/${info.img}`}
                    style={{ width: '100%' }}
                  ></img>
                </Lazyload>
              }
            >
              <AreaChartOutlined />
            </Tooltip>
            {img}
          </React.Fragment>
        );
      },
    },
    {
      title: '所属区域',
      dataIndex: 'labels',
    },
    {
      title: '价格',
      key: 'price',
      dataIndex: 'price',
      render: (item: any) => {
        return item === 0 ? (
          <span>待定</span>
        ) : item < 10 ? (
          <span>{item}万/套</span>
        ) : (
          <span>{item}元/m²</span>
        );
      },
    },
    {
      title: '更新时间',
      dataIndex: 'update_time',
      render: (item) => {
        return timestampToTime(item);
      },
    },
    {
      title: '排序',
      dataIndex: 'ordid',
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: (item) => {
        return (
          <Switch
            checkedChildren="正常"
            unCheckedChildren="禁用"
            defaultChecked={item === 0 ? false : true}
          ></Switch>
        );
      },
    },
    {
      title: '操作',
      valueType: 'option',
      key: 'option',
      render: (text, record, _, action) => {
        return (
          <React.Fragment>
            <Row>
              <Col>
                <Button
                  size="small"
                  style={{
                    color: '#fff',
                    background: '#009688',
                    marginRight: 5,
                    borderRadius: 5,
                    marginBottom: 5,
                  }}
                >
                  户型
                </Button>
              </Col>
              <Col>
                <Button
                  size="small"
                  style={{
                    color: '#fff',
                    background: '#009688',
                    marginRight: 5,
                    borderRadius: 5,
                    marginBottom: 5,
                  }}
                >
                  相册
                </Button>
              </Col>
              <Col>
                <Button
                  size="small"
                  style={{
                    color: '#fff',
                    background: '#009688',
                    marginRight: 5,
                    borderRadius: 5,
                    marginBottom: 5,
                  }}
                >
                  动态
                </Button>
              </Col>
            </Row>
            <Row>
              <Col>
                <Button
                  size="small"
                  style={{
                    color: '#fff',
                    background: '#009688',
                    marginRight: 5,
                    borderRadius: 5,
                    marginBottom: 5,
                  }}
                  onClick={() => {
                    history.push('/house/building/sand', {
                      id: record.id,
                    });
                  }}
                >
                  沙盘
                </Button>
              </Col>
              <Col>
                <Button
                  size="small"
                  style={{
                    color: '#fff',
                    background: '#009688',
                    marginRight: 5,
                    borderRadius: 5,
                    marginBottom: 5,
                  }}
                  onClick={() => {
                    history.push('/house/building/editDetail');
                    props.dispatch({
                      type: 'building/GET_DETAILSLIST',
                      payload: record.id,
                    });
                  }}
                >
                  编辑
                </Button>
              </Col>
              <Col>
                <Button
                  size="small"
                  style={{
                    color: '#fff',
                    background: '#f00',
                    marginRight: 5,
                    borderRadius: 5,
                    marginBottom: 5,
                  }}
                  onClick={() => {
                    del_data(record);
                  }}
                >
                  删除
                </Button>
              </Col>
            </Row>
          </React.Fragment>
        );
      },
    },
  ];
  const [data, setData] = useState(
    JSON.parse(localStorage.getItem('list') as string),
  );
  const actionRef = useRef<ActionType>();
  const [newData, setNewData] = useState([]);

  useEffect(() => {
    props.dispatch({
      type: 'second/init',
    });
  }, []);
  const del_data = (record: any) => {
    props.dispatch({
      type: 'second/del',
      payload: record,
    });
    setData(JSON.parse(localStorage.getItem('list') as string));
  };
  const onkeydown = (e: any) => {
    if (e.keyCode === 13) {
      const arr = data.filter((item: any) => {
        return item.title.includes(e.target.value);
      });
      setNewData(arr);
    }
  };
  return (
    <>
      <input type="text" onKeyDown={onkeydown} />
      <ProTable<GithubIssueItem>
        columns={columns}
        actionRef={actionRef}
        cardBordered
        editable={{
          type: 'multiple',
        }}
        rowSelection={{
          // 自定义选择项参考: https://ant.design/components/table-cn/#components-table-demo-row-selection-custom
          // 注释该行则默认不显示下拉选项
          selections: [Table.SELECTION_ALL, Table.SELECTION_INVERT],
        }}
        dataSource={newData.length > 0 ? newData : data}
        columnsState={{
          persistenceKey: 'pro-table-singe-demos',
          persistenceType: 'localStorage',
        }}
        rowKey="id"
        search={false}
        options={{
          setting: {
            listsHeight: 400,
          },
        }}
        form={{
          // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
          syncToUrl: (values, type) => {
            if (type === 'get') {
              return {
                ...values,
                created_at: [values.startTime, values.endTime],
              };
            }
            return values;
          },
        }}
        pagination={{
          pageSize: 5,
          onChange: (page) => console.log(page),
        }}
        dateFormatter="string"
        headerTitle="高级表格"
        toolBarRender={() => [
          <Button key="button" icon={<PlusOutlined />} type="primary">
            新建
          </Button>,
          <Dropdown
            key="menu"
            menu={{
              items: [
                {
                  label: '1st item',
                  key: '1',
                },
                {
                  label: '2nd item',
                  key: '1',
                },
                {
                  label: '3rd item',
                  key: '1',
                },
              ],
            }}
          >
            <Button>
              <EllipsisOutlined />
            </Button>
          </Dropdown>,
        ]}
      />
    </>
  );
}

const mapStateToProps = (state: any) => {
  return {
    list: state.second.list,
  };
};

export default connect(mapStateToProps)(houseManagement);
